﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections;
using System.Reflection;
using MolineSoftware.DrupalXmlRpc.Interfaces;
using MolineSoftware.DrupalXmlRpc.Classes;
using CookComputing.XmlRpc;

namespace MolineSoftware.DrupalXmlRpc
{
    /// <summary>
    /// Static Class: Contains Generic Static Extention classes.
    /// </summary>
    public static class DrupalNodeExtentionMethods
    {
        /// <summary>
        /// Set Value in <see cref="DrupalNode"/>
        /// </summary>
        /// <param name="node"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <param name="rpcLable"></param>
        public static void SetValue<T>(this T node, string propertyName, object value, string rpcLable)
            where T : DrupalBase
        {
            PropertyInfo pi = node.GetType().GetProperties().Where(p => p.Name.ToUpper() == propertyName.ToUpper()).Single();
            if (pi.PropertyType == typeof(XmlRpcStruct[]))
            {
                XmlRpcStruct[] xList = new XmlRpcStruct[1];
                xList[0] = new XmlRpcStruct();
                xList[0].Add(rpcLable, value);
                pi.SetValue(node, xList, null);
            }
            else if (pi.PropertyType == typeof(XmlRpcStruct))
            {
                XmlRpcStruct x = new XmlRpcStruct();
                x.Add(rpcLable, value);
                pi.SetValue(node, x, null);
            }
            else
                pi.SetValue(node, value, null);
        }

        /// <summary>
        /// Convert <see cref="T"/> to <see cref="XmlRpcStruct"/>
        /// </summary>
        /// <typeparam name="T">base class of <see cref="DrupalBase"/></typeparam>
        /// <param name="node">The <see cref="T"/> to convert</param>
        /// <returns><see cref="XmlRpcStruct"/></returns>
        public static XmlRpcStruct Convert<T>(this T node)  where T : DrupalBase
        {
            XmlRpcStruct xStruct = new XmlRpcStruct();
            KrmSoftware.ClassExtender ext = new KrmSoftware.ClassExtender(node);
            try
            {
                foreach (PropertyInfo pi in node.GetType().GetProperties())
                {
                    try
                    {
                        XmlRpcMemberAttribute att =
                            Attribute.GetCustomAttribute(pi, typeof(XmlRpcMemberAttribute)) as XmlRpcMemberAttribute;

                        if (att == null)
                            continue;

                        object value = ext.GetValue(pi.Name);
                        if(value != null)
                            xStruct.Add(att.Member, value);

                    }
                    catch (Exception ex) 
                    { 
                    
                    }

                }
                return xStruct;

            }
            catch (Exception ex)
            {
                throw ex;
            }


        }


    }
}
